/** 
 * Description:用户积分信息DAO
 * Date:2011-7-12
 * Author:Along	
*/

package com.community.shopping.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.community.shopping.domain.UserScore;
import com.community.shopping.service.logic.Page;



public class UserScoreDao extends HibernateDaoSupport{
	private Log log = LogFactory.getLog(UserScoreDao.class);  
	

	public void save(UserScore userScore){
		getHibernateTemplate().saveOrUpdate(userScore);	
		log.debug("-------保存一行userScore-------");
	}

	public void delete(UserScore userScore){		
		getHibernateTemplate().delete(userScore);
		log.debug("---------删除一行userScore-------");
	}


	public List<?> getUserScores() {  		
	 return getHibernateTemplate().find(" from UserScore"); 
	 }

	public List<?> getUserScores(String sql){
		String finalsql="from UserScore where "+sql;
		return getHibernateTemplate().find(finalsql);
	}   
	
	public List<?> getUserScores(String sql,Page page){
		String finalsql=" from UserScore where "+sql;
		Query   query   = this.getSession().createQuery(finalsql+" order by createDate desc"); 
		query.setFirstResult(page.getPageStartRow()>0?page.getPageStartRow()-1:0); 
		query.setMaxResults(page.getPageSize()); 
		if(page.getTotalRowsAmount()==0)
		{
		page.setTotalRowsAmount(getTotalCount(finalsql));
		}
		List   stu   =   query.list(); 
		return stu;
	} 
	
	public int getTotalCount(String sql)
	{
		Query   query   = this.getSession().createQuery("select count(*) "+sql); 
		int i=Integer.parseInt(query.list().get(0).toString());
		return i;
	}

}